package edu.gatech.oad.mule.input;

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JComponent;

import edu.gatech.oad.mule.input.interfaces.ControllerListener;
import edu.gatech.oad.mule.input.interfaces.ControllerOwner;

/**
 * This class represents an input device for the Player to interact with. This
 * can be a keyboard, xbox 360 controller, joystick, etc.
 * 
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin
 *         Park
 * @version %I%, %G%
 */
public abstract class Controller{

	private ControllerOwner owner;
	private Vector<ControllerListener> _listeners;

	/**
	 * This constructor configures a new Controller object. Controllers are
	 * associated with a single player.
	 */
	public Controller(ControllerOwner owner) {
		this.owner = owner;
	}// end constructor

	/**
	 * Getter for this Controller's owner.
	 * 
	 * @return This Controller's ControllerOwner.
	 */
	public ControllerOwner getOwner() {
		return owner;
	}

	/**
	 * Add's a given ControllerListener to the list of listeners who will be
	 * notified when a controller event occurs.
	 * 
	 * @param listener
	 *            The listener to be added to the list.
	 */
	public final void addActionListener(ControllerListener listener) {
		if (_listeners == null)
			_listeners = new Vector<ControllerListener>();

		_listeners.addElement(listener);
	}
	
	/**
	 * Sends notification to all listeners that a button event has occurred.
	 */
	protected final void buttonEvent(InputEvent event) {
		if (_listeners != null && !_listeners.isEmpty()) {
			Enumeration<ControllerListener> listeners = _listeners.elements();
			while (listeners.hasMoreElements()) {
				ControllerListener listener = (ControllerListener) listeners
						.nextElement();
				listener.inputEventHandler(event, this);
			}
		}
	}
}// end class Controller

// https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
// http://stackoverflow.com/questions/14293956/how-to-know-what-are-the-input-devices-connected-to-the-system. 